package com.fzt.admin.controller;

import com.fzt.admin.common.convention.result.Result;
import com.fzt.admin.common.convention.result.Results;
import com.fzt.admin.dto.req.UserLoginReqDTO;
import com.fzt.admin.dto.req.UserRegisterDTO;
import com.fzt.admin.dto.req.UserUpdateReqDTO;
import com.fzt.admin.dto.resp.UserLoginRespDTO;
import com.fzt.admin.dto.resp.UserRespDTO;
import com.fzt.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor //构造器注入
public class UserController {

    private final UserService userService;
    /*@Autowired
    UserService userService;*/

    /*
    根据用户名查询用户
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable String username){
        return userService.getUserByUsername(username);
    }

    /*
    查询用户名是否存在
    不存在返回true
    存在返回false
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username) {
        return Results.success(userService.hasUsername(username));
    }

    /*
    用户注册
     */
    @PostMapping("/api/short-link/admin/v1/user/register")
    public Result<Void> register(@RequestBody UserRegisterDTO userRegisterDTO) {
        userService.register(userRegisterDTO);
        return Results.success();
    }

    /*
    用户修改
     */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> userUpdate(@RequestBody UserUpdateReqDTO userUpdateReqDTO){
        userService.updateByUsername(userUpdateReqDTO);
        return Results.success();
    }

    /*
    用户登入
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO userLoginReqDTO){
        UserLoginRespDTO result = userService.login(userLoginReqDTO);
        return Results.success(result);
    }

    /*
    检查用户是否登入
     */
    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("token") String token, @RequestParam("username") String username){
        return Results.success(userService.checkLogin(token, username));
    }

    /*
    退出登入
     */
    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("token") String token, @RequestParam("username") String username){
        userService.logout(token, username);
        return Results.success();
    }
}
